package dao.mysql;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import model.PriorityDTO;
import dao.DAOException;
import dao.PriorityDAO;

public class PriorityDAOImpl extends BaseDAO implements PriorityDAO {
	public PriorityDAOImpl() {
		super();
	}
	
	private ArrayList<PriorityDTO> getList(PreparedStatement ps) throws SQLException {
		ArrayList<PriorityDTO> list = new ArrayList<PriorityDTO>();
		
		PriorityDTO data = null;
		
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			data = new PriorityDTO();
			data.setCode(rs.getInt("Code"));
			data.setLevel(rs.getString("Level"));
			list.add(data);
		}
		rs.close();
		
		return list;
	}
	
	public ArrayList<PriorityDTO> getAll() throws DAOException {
		openConnection(false);

		ArrayList<PriorityDTO> list = new ArrayList<PriorityDTO>();
		try {
			String sql = "select * from Priority";
			PreparedStatement prep = this.getPreparedStatement(sql);
			list = getList(prep);
			prep.close();
		} catch (Exception e) {
			String error = "Error on Get All Priority. Nested Exception is: "
					+ e;
			super.printLog(error, Level.SEVERE);
			throw new DAOException(error);
		} finally {
			closeConnection();
		}
		return list;
	}
	
	public PriorityDTO getMaxPriority() throws DAOException {
		openConnection(false);

		PriorityDTO data = null;

		try {
			String sql = "select * from Priority Order By Code DESC ";
			PreparedStatement preq = getPreparedStatement(sql);
			ResultSet rs = preq.executeQuery();
			if (rs.next()) {
				data = new PriorityDTO();
				data.setCode(rs.getInt("Code"));
				data.setLevel(rs.getString("Level"));
			}

			rs.close();
			preq.close();
		} catch (Exception e) {
			String error = "Error on GetMaxPriority. Nested Exception is: " + e;
			super.printLog(error, Level.SEVERE);
			throw new DAOException(error);
		} finally {
			closeConnection();
		}

		return data;
	}
	
	public PriorityDTO getPriority(int code) throws DAOException {
		openConnection(false);

		PriorityDTO data = null;

		try {
			String sql = "select * from Priority where Code = ? ";
			PreparedStatement preq = getPreparedStatement(sql);
			preq.setInt(1, code);
			ResultSet rs = preq.executeQuery();
			if (rs.next()) {
				data = new PriorityDTO();
				data.setCode(rs.getInt("Code"));
				data.setLevel(rs.getString("Level"));
			}

			rs.close();
			preq.close();
		} catch (Exception e) {
			String error = "Error on GetPriority. Nested Exception is: " + e;
			super.printLog(error, Level.SEVERE);
			throw new DAOException(error);
		} finally {
			closeConnection();
		}

		return data;
	}
	
	@Override
	public void insertPriority(PriorityDTO data) throws DAOException {
		openConnection(false);

		try {
			String sql = "INSERT INTO Priority " + 
		                 " ( Code, Level ) " +
						 " VALUES ( ?, ? ) ";
			PreparedStatement prep = getPreparedStatement(sql);
			prep.setInt(1, data.getCode());
			prep.setString(2, data.getLevel());
			
			prep.executeUpdate();

			printLog("Executing Insert: " + sql + "[Values: " + data.getCode() + ", " + data.getLevel() + "]", Level.INFO);

			commit();
			prep.close();
		} catch (Exception e) {
			String error = "Error Inserting Priority. Nested Exception is: " + e;
			printLog(error, Level.SEVERE);
			throw new DAOException(error);
		} finally {
			closeConnection();
		}
	}
	
	@Override
	public void updatePriority(PriorityDTO data) throws DAOException {
		openConnection(false);

		try {
			String sql = "UPDATE Priority SET Level = ? WHERE (Code = ? ) ";			
			PreparedStatement prep = getPreparedStatement(sql);
			prep.setString(1, data.getLevel()); 
			prep.setInt(2, data.getCode());
					
			prep.executeUpdate();

			commit();
			prep.close();
		} catch (Exception e) {
			String error = "Error Updating Priority. Nested Exception is: " + e;
			printLog(error, Level.SEVERE);
			throw new DAOException(error);
		} finally {
			closeConnection();
		}
	}
	
	@Override
	public void deletePriority(PriorityDTO data) throws DAOException {
			openConnection(false);
		
		try {
			String sql = "DELETE FROM Priority WHERE (Code = ?)";
			PreparedStatement prep = getPreparedStatement(sql);
			prep.setInt(1, data.getCode());

			prep.executeUpdate();

			commit();
			prep.close();
		} catch (Exception e) {
			String error = "Error Deleting Priority. Nested Exception is: " + e;
			printLog(error, Level.SEVERE);
			throw new DAOException(error);
		} finally {
			closeConnection();
		}
	}
}
